package com.fafa.pos.dao.impl;

import android.content.ContentValues;
import android.database.Cursor;
import com.fafa.pos.dao.DBUtil;
import com.fafa.pos.dao.IConsumeInfoDao;
import com.fafa.pos.pojo.ConsumeInfo;
import com.fafa.pos.util.DateUtil;
import com.fafa.pos.vo.CurUser;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zhou Bing
 *
 * on 2017/8/16.
 */
public class ConsumeInfoDaoImpl implements IConsumeInfoDao {

    private DBUtil dbUtil;

    public ConsumeInfoDaoImpl(DBUtil dbUtil) {
        this.dbUtil = dbUtil;
    }

    @Override
    public void saveConsumeInfo(ConsumeInfo consumeInfo) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("ID", consumeInfo.getId());
        contentValues.put("CARD_CODE", consumeInfo.getCardCode());
        contentValues.put("VIP_NAME", consumeInfo.getVipName());
        contentValues.put("MONEY", consumeInfo.getMoney());
        contentValues.put("DATE", consumeInfo.getDate());
        contentValues.put("ISUPLOAD", consumeInfo.getIsUpLoad());
        if(consumeInfo.getErrorMsg()!=null){
            contentValues.put("ERRORMSG", consumeInfo.getErrorMsg());
        }else{
            contentValues.put("ERRORMSG", "");
        }
        dbUtil.getDB().insert("T_CONSUMEINFO", null, contentValues);
        dbUtil.close();
    }

    @Override
    public void saveErrorMsg(String id,String errorMsg) {

        String sql = "update T_CONSUMEINFO set ERRORMSG = ? where ID = ?";
        dbUtil.getDB().execSQL(sql,new String[]{errorMsg,id});
        dbUtil.close();
    }

    @Override
    public void setIsUpLoad(String id, String upload) {
        String sql = "update T_CONSUMEINFO set ISUPLOAD = ? where ID = ?";
        dbUtil.getDB().execSQL(sql,new String[]{upload,id});
        dbUtil.close();
    }

    @Override
    public List<ConsumeInfo> getCurDayConsumeInfo() {

        List<ConsumeInfo> consumeInfos = new ArrayList<ConsumeInfo>();
        String sql = "select * from T_CONSUMEINFO where DATE  like ? order by DATE desc";
        Cursor cursor = dbUtil.getDB().rawQuery(sql, new String[]{DateUtil.getCurDateStr("yyyy.MM.dd")+"%"});
        consumeInfos = getList(cursor);
        if (cursor != null) {
            cursor.close();
        }
        dbUtil.close();
        return consumeInfos;
    }

    @Override
    public List<ConsumeInfo> getCurDayOffLineConsumeInfo(String cardCode) {
        List<ConsumeInfo> consumeInfos = new ArrayList<ConsumeInfo>();
        String sql = "select * from T_CONSUMEINFO where CARD_CODE = ? and DATE  like ? and ISUPLOAD='0'  order by DATE desc";
        Cursor cursor = dbUtil.getDB().rawQuery(sql, new String[]{cardCode, DateUtil.getCurDateStr("yyyy.MM.dd")+"%"});
        consumeInfos = getList(cursor);
        if (cursor != null) {
            cursor.close();
        }
        dbUtil.close();
        return consumeInfos;
    }

    @Override
    public List<ConsumeInfo> getAllOffLineConsumeInfo() {

        List<ConsumeInfo> consumeInfos = new ArrayList<ConsumeInfo>();
        String sql = "select * from T_CONSUMEINFO where ISUPLOAD='0'  order by DATE desc";
        Cursor cursor = dbUtil.getDB().rawQuery(sql, null);
        consumeInfos = getList(cursor);
        if (cursor != null) {
            cursor.close();
        }
        dbUtil.close();
        return consumeInfos;
    }


    private List<ConsumeInfo> getList(Cursor cursor){
        List<ConsumeInfo> consumeInfos = new ArrayList<ConsumeInfo>();
        while (cursor.moveToNext()) {
            ConsumeInfo consumeInfo = new ConsumeInfo();
            consumeInfo.setId(cursor.getString(0));
            consumeInfo.setCardCode(cursor.getString(1));
            consumeInfo.setVipName(cursor.getString(2));
            consumeInfo.setMoney(cursor.getString(3));
            consumeInfo.setDate(cursor.getString(4));
            consumeInfo.setIsUpLoad(cursor.getString(5));
            consumeInfo.setErrorMsg(cursor.getString(6));
            consumeInfos.add(consumeInfo);
        }
        return consumeInfos;
    }
}
